import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import { spaceid } from './config';
import p001 from './p001';
import p002 from './p002';

// import remove from '@dfeidao/atom-web/local/remove';
// import hide from '@dfeidao/atom-web/ui/hide';
export default async function a001(fd: IFeidaoAiBrowserComponent, args: { tabname: string, arr: string[], role_type_id: string }) {
	// 初始化
	fd.data.node.classList.remove('display-n');
	const all_boxs = fd.data.node.querySelectorAll('[data-feidao-name="list_checkbox"]');
	Array.from(all_boxs).forEach((v: HTMLInputElement) => {
		v.checked = false;
	});
	fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="checkall"]').checked = false;
	// role_type_id
	const role_type_id = args.role_type_id;
	set(fd, 'role_type_id', role_type_id);
	const tablename = args.tabname;
	const oldarr = args.arr;
	// console.log(oldarr, '000');
	const query = {
		filter: {
			tablename,
			productid: spaceid
		}
	};
	// console.log(query, '---');
	const res = await nodejs<{
		length: number; dataArray: Array<{
			file_cont: string
		}>; page_size: number; page_no: number
	}>('role-type/zj-000005/s001', query);
	// console.log(res.dataArray.length, '111');
	// debugger;
	const p2 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p002"]');
	const p1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
	// p2.classList.add('display-n');
	const obj = JSON.parse(res.dataArray[0].file_cont);
	// console.log(obj.fields.dataArray, '---');
	const newarr = new Array();
	if (res.dataArray.length > 0 && obj.fields.dataArray.length > 0) {

		// let flag = true;
		obj.fields.dataArray.forEach((v: { field: string, fieldtitle: string, fieldtype: string }) => {
			let ins = 0;
			oldarr.forEach((item) => {
				if (v.field !== item) {
					ins++;
				}
				if (ins === oldarr.length) {
					newarr.push(v);
				}
			});
		});
		// console.log(newarr, 'newarr');
		if (newarr.length === 0 && oldarr.length > 0) {
			p1.classList.add('display-n');

			p2.classList.remove('display-n');
			// render(fd, obj.fields.dataArray, p001, 'p001', 'inner');
			render(fd, '', p002, 'p002', 'inner');
		} else if (newarr.length === 0 && oldarr.length <= 0) {
			p1.classList.remove('display-n');

			p2.classList.add('display-n');
			render(fd, obj.fields.dataArray, p001, 'p001', 'inner');
		} else {
			p2.classList.add('display-n');
			p1.classList.remove('display-n');

			render(fd, newarr, p001, 'p001', 'inner');
		}




	} else {
		p1.classList.add('display-n');

		p2.classList.remove('display-n');

		render(fd, '', p002, 'p002', 'inner');

	}

	// console.log(newarr, '--');
	// const secobj = {};
	// const c = newarr.reduce((cur, next) => {
	// 	secobj[next.field] ? window.console.log() : secobj[next.field] = true && cur.push(next);
	// 	return cur;
	// }, []);
	// console.log(c, '--');

}
